package com.hnxxxy.workshop.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hnxxxy.workshop.mapper.WorkshopMapper;
import com.hnxxxy.workshop.model.DepotDO;
import com.hnxxxy.workshop.model.WorkshopDO;
import com.hnxxxy.workshop.request.WorkshopPageRequest;
import com.hnxxxy.workshop.request.WorkshopRequest;
import com.hnxxxy.workshop.service.WorkshopService;
import com.hnxxxy.workshop.vo.DepotVO;
import com.hnxxxy.workshop.vo.WorkshopVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 车间 服务实现类
 * </p>
 *
 * @author zqh
 * @since 2022-05-09
 */
@Service
@Slf4j
public class WorkshopServiceImpl implements WorkshopService {

    @Resource
    private WorkshopMapper workshopMapper;

    @Override
    public void add(WorkshopRequest workshopRequest) {
        WorkshopDO workshopDO = new WorkshopDO();
        BeanUtils.copyProperties(workshopRequest, workshopDO);

        int rows = workshopMapper.insert(workshopDO);
        log.info("新增消息:rows={},data={}",rows,workshopDO);
    }

    @Override
    public void deleteById(int id) {
        int rows = workshopMapper.deleteById(id);
        log.info("删除消息条数 :rows={}",rows);
    }

    @Override
    public void update(WorkshopRequest workshopRequest) {
        WorkshopDO workshopDO = new WorkshopDO();
        BeanUtils.copyProperties(workshopRequest, workshopDO);

        int rows = workshopMapper.updateById(workshopDO);
        log.info("修改消息:rows={},data={}",rows,workshopDO);
    }


    @Override
    public WorkshopVO findByIdWorkshop(int id) {
        WorkshopVO workshopVO = new WorkshopVO();

        WorkshopDO workshopDO = workshopMapper.selectById(id);
        BeanUtils.copyProperties(workshopDO, workshopVO);
        log.info("根据id查询信息:id={}  workshopVO={}",id,workshopVO);
        return workshopVO;
    }


    @Override
    public Map<String, Object> page(WorkshopPageRequest workshopPageRequest) {
        int page = workshopPageRequest.getPage();
        int size = workshopPageRequest.getSize();

        String name = workshopPageRequest.getName();
        String director = workshopPageRequest.getDirector();
        QueryWrapper<WorkshopDO> queryWrapper = new QueryWrapper<WorkshopDO>();

        if (!StrUtil.hasEmpty(name)){
            queryWrapper.like("name", name);
        }
        if (!StrUtil.hasEmpty(director)){
            queryWrapper.like("director", director);
        }

        Page<WorkshopDO> pageInfo = new Page<>(page, size);

        IPage<WorkshopDO> workshopDOIPage = workshopMapper.selectPage(pageInfo,queryWrapper);

        Map<String,Object> pageMap = new HashMap<>(3);

        pageMap.put("total_record", workshopDOIPage.getTotal());
        pageMap.put("total_page", workshopDOIPage.getPages());
        pageMap.put("current_data",workshopDOIPage.getRecords()
                .stream().map(obj->beanProocess(obj)).collect(Collectors.toList()));

        log.info("分页消息:pageMap={}",pageMap);
        return pageMap;

    }

    private WorkshopVO beanProocess(WorkshopDO obj) {
        WorkshopVO workshopVO = new WorkshopVO();
        BeanUtils.copyProperties(obj, workshopVO);
        return workshopVO;
    }

}
